package com.aaron.gesturehelper.gesturelib;

import java.io.File;

import com.aaron.gesturehelper.R;
import com.aaron.gesturehelper.gesturelib.GestureView.OnGestureListener;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CreateGestureActivity extends Activity implements OnGestureListener, OnClickListener {
	
	private static final String TAG = "TestGestureActivity";
	private GestureView mView;
	private EditText mText;
	private Button mAdd;
	private MyGesture mGesture;
	private MyGestureLibrary mLib;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.creategesture);
		
		mView = (GestureView) findViewById(R.id.gestureview);
		mView.setGestureListener(this);
		
		mText = (EditText) findViewById(R.id.gesturename);
		mAdd = (Button) findViewById(R.id.add);
		mAdd.setOnClickListener(this);
		
		mLib = MyGestureLibrary.fromFile(new File(getFilesDir(),"gestures"));
		mLib.load();
	}
	
	@Override
	public void onGesture(GestureView view) {
		Log.i(TAG,"onGesture");
	}
	@Override
	public void onGestureEnd(GestureView view) {
		Log.i(TAG,"onGestureEnd");
		mGesture = view.getGesture();
	}
	@Override
	public void onGestureStart(GestureView view) {
		Log.i(TAG,"onGestureStart");
	}
	@Override
	public void onClick(View v) {
		String text;
		Editable name = mText.getText();
		if(name != null && !(text = name.toString()).equals("") && mGesture != null) {
			mLib.addGesture(text, mGesture);
		}
		mGesture = null;
	}
	
	public void onDestroy() {
		mLib.save();
		mLib = null;
		super.onDestroy();
	}
}
